using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using SolanumSolstice.Units;
using SolanumSolstice.Mapping;
 
namespace SolanumSolstice.Buildings
{
    public class SpawnChooser
    {
        private Texture2D background;
        private Texture2D civEmblem;
        private Texture2D milEmblem;
        private Texture2D sciEmblem;
        private Texture2D civHUD;
        private Texture2D copHUD;
        private Texture2D resHUD;
        private Texture2D subHUD;
        private Texture2D survHUD;
        private Texture2D solHUD;
        private Texture2D specHUD;
        private Texture2D scholHUD;
        private Texture2D genHUD;
        private SpriteFont font;
        private SpriteFont smallFont;
        private bool xIsPressed;
        /*
        still need subsister, survivalist, soldier, spec op, scholar, genius
         */
        private Beacon beacon;
        private int civStartX = 220;
        private int civStartY = 200;
        private int milStartX = 320;
        private int milStartY = 200;
        private int sciStartX = 420;
        private int sciStartY = 200;


        public SpawnChooser(ContentManager content, Beacon beacon)
        {
            this.beacon = beacon;
            background = content.Load<Texture2D>("Content\\Txtr\\Sys\\ResearchTree\\researchTreeBG");
            civEmblem = content.Load<Texture2D>("Content\\Txtr\\Units\\HUD\\civHUD");
            milEmblem = content.Load<Texture2D>("Content\\Txtr\\Units\\HUD\\copHUD");
            sciEmblem = content.Load<Texture2D>("Content\\Txtr\\Units\\HUD\\researcherHUD");
            civHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\civ_civilian_sheet");
            copHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\mil_cop_sheet");
            resHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\sci_researcher_sheet");
            subHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\civ_subsistor_sheet");
            survHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\civ_survivalist_sheet");
            solHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\mil_soldier_sheet");
            specHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\mil_special_ops_sheet");
            scholHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\sci_scholar_sheet");
            genHUD = content.Load<Texture2D>("Content\\Txtr\\Units\\Sprites\\sci_genius_sheet");
            font = content.Load<SpriteFont>("Content\\Fnt\\ArialMedium");
            smallFont = content.Load<SpriteFont>("Content\\Fnt\\ArialSmall");
            xIsPressed = false;

        }

        public void Update(GameTime gameTime, GamePadState gamePad, GameCursor gameCursor)
        {
            Base theBase = SolanumSolstice.GetBase();

            if (gamePad.Buttons.X == ButtonState.Pressed && xIsPressed == false)
            {
                xIsPressed = true;
                if (gameCursor.ScreenPositionX >= civStartX && gameCursor.ScreenPositionX <= civStartX + 40)
                {
                    if (gameCursor.ScreenPositionY >= civStartY && gameCursor.ScreenPositionY <= civStartY + 40)
                    {
                        if (theBase.Food >= beacon.CivArchetype.SpawnCost)
                        {
                            beacon.SpawnCivilian();
                            theBase.UseFood(beacon.CivArchetype.SpawnCost);
                        }
                    }
                    else if (gameCursor.ScreenPositionY >= civStartY + 80 && gameCursor.ScreenPositionY <= civStartY + 120)
                    {
                        if (beacon.SubsistorIsLocked == false && theBase.Food >= beacon.SubArchetype.SpawnCost)
                        {
                            beacon.SpawnSubsister();
                            theBase.UseFood(beacon.SubArchetype.SpawnCost);
                        }
                    }
                    else if (gameCursor.ScreenPositionY >= civStartY + 160 && gameCursor.ScreenPositionY <= civStartY + 200)
                    {
                        if (beacon.SurvivalistIsLocked == false && theBase.Food >= beacon.SurvArchetype.SpawnCost)
                        {
                            beacon.SpawnSurvivalist();
                            theBase.UseFood(beacon.SurvArchetype.SpawnCost);
                        }
                    }
                }
                else if (gameCursor.ScreenPositionX >= milStartX && gameCursor.ScreenPositionX <= milStartX + 40)
                {
                    if (gameCursor.ScreenPositionY >= milStartY && gameCursor.ScreenPositionY <= milStartY + 40)
                    {
                        if (theBase.Food >= beacon.CopArchetype.SpawnCost)
                        {
                            beacon.SpawnCop();
                            theBase.UseFood(beacon.CopArchetype.SpawnCost);
                        }
                    }
                    else if (gameCursor.ScreenPositionY >= milStartY + 80 && gameCursor.ScreenPositionY <= milStartY + 120)
                    {
                        if (beacon.SoldierIsLocked == false && theBase.Food >= beacon.SldArchetype.SpawnCost)
                        {
                            beacon.SpawnSoldier();
                            theBase.UseFood(beacon.SldArchetype.SpawnCost);
                        }
                    }
                    else if (gameCursor.ScreenPositionY >= milStartY + 160 && gameCursor.ScreenPositionY <= milStartY + 200)
                    {
                        if (beacon.SpecOpIsLocked == false && theBase.Food >= beacon.SoArchetype.SpawnCost)
                        {
                            beacon.SpawnSpecialOps();
                            theBase.UseFood(beacon.SoArchetype.SpawnCost);
                        }
                    }
                }
                else if (gameCursor.ScreenPositionX >= sciStartX && gameCursor.ScreenPositionX <= sciStartX + 40)
                {
                    if (gameCursor.ScreenPositionY >= sciStartY && gameCursor.ScreenPositionY <= sciStartY + 40)
                    {
                        if (theBase.Food >= beacon.RschArchetype.SpawnCost)
                        {
                            beacon.SpawnResearcher();
                            theBase.UseFood(beacon.RschArchetype.SpawnCost);
                        }
                    }
                    else if (gameCursor.ScreenPositionY >= sciStartY + 80 && gameCursor.ScreenPositionY <= sciStartY + 120)
                    {
                        if (beacon.ScholarIsLocked == false && theBase.Food >= beacon.SchArchetype.SpawnCost)
                        {
                            beacon.SpawnScholar();
                            theBase.UseFood(beacon.SchArchetype.SpawnCost);
                        }
                    }
                    else if (gameCursor.ScreenPositionY >= sciStartY + 160 && gameCursor.ScreenPositionY <= sciStartY + 200)
                    {
                        if (beacon.GeniusIsLocked == false && theBase.Food >= beacon.GenArchetype.SpawnCost)
                        {
                            beacon.SpawnGenius();
                            theBase.UseFood(beacon.GenArchetype.SpawnCost);
                        }
                    }
                }
            }
            else if (gamePad.Buttons.X == ButtonState.Released)
            {
                xIsPressed = false;
            }

        }

        public void Draw(SpriteBatch spritebatch)
        {

            Base theBase = SolanumSolstice.GetBase();

            spritebatch.Draw(background, new Rectangle(150, 50, 400, 400), Color.White);

            spritebatch.DrawString(font, "Choose a Unit", new Vector2(275, 100), Color.White);

            //Draw Civ Stuff
            spritebatch.Draw(civEmblem, new Rectangle(civStartX - 5, civStartY - 60, 50, 50), Color.White);

            if (theBase.Food >= beacon.CivArchetype.SpawnCost)
            {
                spritebatch.Draw(civHUD, new Rectangle(civStartX, civStartY, 40, 40), new Rectangle(32, 64, 32, 32), Color.White);
            }
            else
            {
                spritebatch.Draw(civHUD, new Rectangle(civStartX, civStartY, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.CivArchetype.SpawnCost, new Vector2(civStartX + 42, civStartY + 5), Color.Gold);


            if (beacon.SubsistorIsLocked == false && theBase.Food >= beacon.SubArchetype.SpawnCost)
            {
                spritebatch.Draw(subHUD, new Rectangle(civStartX, civStartY + 80, 40, 40), new Rectangle(32, 64, 32, 32), Color.White);
            }
            else
            {
                spritebatch.Draw(subHUD, new Rectangle(civStartX, civStartY + 80, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.SubArchetype.SpawnCost, new Vector2(civStartX + 42, civStartY + 80 + 5), Color.Gold);


            if (beacon.SurvivalistIsLocked == false && theBase.Food >= beacon.SurvArchetype.SpawnCost)
            {
                spritebatch.Draw(survHUD, new Rectangle(civStartX, civStartY + 160, 40, 40), new Rectangle(32, 64, 32, 32), Color.White);
            }
            else
            {
                spritebatch.Draw(survHUD, new Rectangle(civStartX, civStartY + 160, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.SurvArchetype.SpawnCost, new Vector2(civStartX + 42, civStartY + 160 + 5), Color.Gold);



            //Draw Mil Stuff

            spritebatch.Draw(milEmblem, new Rectangle(milStartX - 5, milStartY - 60, 50, 50), Color.White);

            if (theBase.Food >= beacon.CopArchetype.SpawnCost)
            {
                spritebatch.Draw(copHUD, new Rectangle(milStartX, milStartY, 40, 40), new Rectangle(32, 64, 32, 32), Color.White);
            }
            else
            {
                spritebatch.Draw(copHUD, new Rectangle(milStartX, milStartY, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.CopArchetype.SpawnCost, new Vector2(milStartX + 42, milStartY + 5), Color.Gold);


            if (beacon.SoldierIsLocked == false && theBase.Food >= beacon.SldArchetype.SpawnCost)
            {
                spritebatch.Draw(solHUD, new Rectangle(milStartX, milStartY + 80, 40, 40),  new Rectangle(32, 64, 32, 32),Color.White);
            }
            else
            {
                spritebatch.Draw(solHUD, new Rectangle(milStartX, milStartY + 80, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.SldArchetype.SpawnCost, new Vector2(milStartX + 42, milStartY + 80 + 5), Color.Gold);


            if (beacon.SpecOpIsLocked == false && theBase.Food >= beacon.SoArchetype.SpawnCost)
            {
                spritebatch.Draw(specHUD, new Rectangle(milStartX, milStartY + 160, 40, 40), new Rectangle(32, 64, 32, 32), Color.White);
            }
            else
            {
                spritebatch.Draw(specHUD, new Rectangle(milStartX, milStartY + 160, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.SoArchetype.SpawnCost, new Vector2(milStartX + 42, milStartY + 160 + 5), Color.Gold);

            //Draw Sci Stuff

            spritebatch.Draw(sciEmblem, new Rectangle(sciStartX - 5, sciStartY - 60, 50, 50), Color.White);

            if (theBase.Food > beacon.RschArchetype.SpawnCost)
            {
                spritebatch.Draw(resHUD, new Rectangle(sciStartX, sciStartY, 40, 40), new Rectangle(32, 64, 32, 32), Color.White);
            }
            else
            {
                spritebatch.Draw(resHUD, new Rectangle(sciStartX, sciStartY, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.RschArchetype.SpawnCost, new Vector2(sciStartX + 42, sciStartY + 5), Color.Gold);

            if (beacon.ScholarIsLocked == false && theBase.Food >= beacon.SchArchetype.SpawnCost)
            {
                spritebatch.Draw(scholHUD, new Rectangle(sciStartX, sciStartY + 80, 40, 40),  new Rectangle(32, 64, 32, 32),Color.White);
            }
            else
            {
                spritebatch.Draw(scholHUD, new Rectangle(sciStartX, sciStartY + 80, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.SchArchetype.SpawnCost, new Vector2(sciStartX + 42, sciStartY + 80 + 5), Color.Gold);

            if (beacon.GeniusIsLocked == false && theBase.Food >= beacon.GenArchetype.SpawnCost)
            {
                spritebatch.Draw(genHUD, new Rectangle(sciStartX, sciStartY + 160, 40, 40),  new Rectangle(32, 64, 32, 32),Color.White);
            }
            else
            {
                spritebatch.Draw(genHUD, new Rectangle(sciStartX, sciStartY + 160, 40, 40), new Rectangle(32, 64, 32, 32), Color.DarkRed);
            }

            spritebatch.DrawString(smallFont, "Cost: " + beacon.GenArchetype.SpawnCost, new Vector2(sciStartX + 42, sciStartY + 160 + 5), Color.Gold);


        }
    }
}

